#!/usr/bin/env python
# coding=utf-8
"""
@Author  : youjia - 卞志伟
@file    : day_report_room_one.py
@contact : bianzhiwei@iyoujia.com
@time    : 2019-09-24 17:12 
@Desc    : 
@Software: PyCharm
"""
import os
import sys

# 当前文件的路径
pwd, filename = os.path.split(os.path.abspath(__file__))
# 当前文件的父路径
father_path = os.path.abspath(os.path.dirname(pwd) + os.path.sep + ".")
# 当前文件的前两级目录
grader_father = os.path.abspath(os.path.dirname(pwd) + os.path.sep + "..")
sys.path.append(pwd)
sys.path.append(father_path)
sys.path.append(grader_father)

import pandas as pd
from report_system.utils import db_util
from report_system.utils import mail_util
from report_system.utils import mailbox_util
from report_system.utils.log_util import log
from report_system.utils.development_util import debug

if debug:
    receivers = 'bianzhiwei@iyoujia.com;yy@iyoujia.com'
    city_mails = mailbox_util.mails_test
else:
    receivers = 'dt@iyoujia.com;yy@iyoujia.com'
    city_mails = mailbox_util.mails_all


def process():
    con = db_util.mongodb_conn("platform_room", "platform_room", db_util.mongodb_url_)

    all_unit = list()

    for unit in con.find():
        room = dict()
        room['评论数'] = unit['comments'] if "comments" in unit else 0
        room['差评数'] = unit['bad_comments'] if "bad_comments" in unit else 0
        room['好评数'] = unit['good_comments'] if "good_comments" in unit else 0
        room['渠道'] = unit['platform'] if "platform" in unit else 0
        room['标题'] = unit['title'] if "title" in unit else ""
        room['第三方id'] = unit['third_id'] if "third_id" in unit else ""
        room['url'] = unit['link'] if "link" in unit else 0
        room['房源评分'] = unit['rate'] if "rate" in unit else 0
        room['是否在线'] = '在线' if unit['online'] == 1 else "不在线" if "online" in unit else "不在线"

        room['新房优惠'] = unit['is_new_house_discount'] if "is_new_house_discount" in unit else ""
        room['超站房东'] = unit['is_superstar'] if "is_superstar" in unit else ""
        room['优选'] = unit['is_optimization'] if "is_optimization" in unit else ""
        room['连住优惠'] = unit['is_multi_day_discount'] if "is_multi_day_discount" in unit else ""
        room['当日特价'] = unit['is_discount'] if "is_discount" in unit else ""

        room['价格'] = unit['price'] if "price" in unit else ""
        room['账号'] = unit['account'] if "account" in unit else ""
        room['居室'] = unit['rooms'] if "rooms" in unit else ""
        room['城市'] = unit['city'] if "city" in unit else ""
        room['服务中心'] = unit['server_name'] if "server_name" in unit else ""
        all_unit.append(room)
    df = pd.DataFrame(all_unit)

    # excel_util.pd_to_excel({"一居房源": df}, "一居房源")

    df_dict = {'一居房源': df}

    text = """    Dear All:
                附件是 一居房源！ 
                请查收！
                谢谢！
        如有问题！
        请联系: dt@iyoujia.com
        """
    mail_util.distribute_mail(df_dict=df_dict, file_name='一居房源', text=text,
                              title="一居房源", receiver=receivers, na_rep='-', uuid='online')


def run():
    try:
        process()
    except Exception as e:
        mail_util.error_mail("一居房源", e.__str__())
        log.exception(e)


if __name__ == '__main__':
    run()
